﻿using System;

namespace Macds.Core.Log
{
    public static class LogFactory
    {
        /// <summary>
        /// Get log4net Logger
        /// </summary>
        /// <returns>log4net.ILog configured as default</returns>
        public static log4net.ILog GetLog4netLogger()
        {
            return GetLog4netLogger(0, "", true);
        }

        /// <summary>
        /// Get log4net Logger
        /// </summary>
        /// <param name="name">Logger name</param>
        /// <returns>log4net.ILog</returns>
        public static log4net.ILog GetLog4netLogger(string name)
        {
            return GetLog4netLogger(0, name, true);
        }

        /// <summary>
        /// Get log4net Logger
        /// </summary>
        /// <param name="name">Logger name</param>
        /// <param name="useDefaultConfig">True or False</param>
        /// <returns>log4net.ILog</returns>
        public static log4net.ILog GetLog4netLogger(string name, bool useDefaultConfig)
        {
            return GetLog4netLogger(0, name, useDefaultConfig);
        }

        /// <summary>
        /// Get log4net Logger
        /// </summary>
        /// <param name="applicationID">Unique ID of the application</param>
        /// <param name="name">Logger name</param>
        /// <param name="useDefaultConfig">True or False</param>
        /// <returns>log4net.ILog</returns>
        public static log4net.ILog GetLog4netLogger(int applicationID, string name, bool useDefaultConfig)
        {
            if (applicationID > 0)
                RegisterApplication(applicationID);
            
            if (useDefaultConfig)
            {
                log4net.Config.BasicConfigurator.Configure(GetDefaultConfig(name));

                if (string.IsNullOrEmpty(name))
                    return log4net.LogManager.GetLogger(typeof(LogFactory));
            }
            
            return log4net.LogManager.GetLogger(name);
        }

        private static void RegisterApplication(int applicationID)
        {
            log4net.ThreadContext.Properties["ApplicationId"] = applicationID;
        }

        private static log4net.Appender.RollingFileAppender GetDefaultConfig(string name)
        {
            if (string.IsNullOrEmpty(name))
                name = "RollingFile";

            log4net.Appender.RollingFileAppender appender = new log4net.Appender.RollingFileAppender();
            appender.Name = "RollingFile";
            appender.File = "App_Data\\logs\\" + name + ".log";
            appender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date;
            appender.DatePattern = "yyyyMMdd";
            appender.AppendToFile = true;
            appender.StaticLogFileName = true;
            appender.Threshold = log4net.Core.Level.All;

            log4net.Layout.PatternLayout layout = new log4net.Layout.PatternLayout("%date [%thread] %-5level %logger - %message%newline");
            appender.Layout = layout;
            appender.ActivateOptions();

            return appender;
        }
    }
}
